Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Read system environment variable

80 views
Skip to first unread message

mat...@my-deja.com

unread,
Jun 7, 2000, 3:00:00 AM6/7/00
to
hi all,

is there a way to read/write system environment variable through WMI
scripting ?

thanks.

Olivier


Sent via Deja.com http://www.deja.com/
Before you buy.

mat...@my-deja.com

unread,
Jun 7, 2000, 3:00:00 AM6/7/00
to
Obviously, there is one.
But I'm not able to get direct access to a specific variable :

GetObject("WinMgmts:/root/cimv2:Win32_Environment.Name=""PATH""") fails
with "automation error"

In article <8hlal1$o8p$1...@nnrp1.deja.com>,

DFENS

unread,
Jun 7, 2000, 3:00:00 AM6/7/00
to
When you use GetObject like this, you need to specify all keys for the
class. Win32_Environment has 2 keys, Name and UserName. Name is the name
of the actual setting (e.g., "Path"). UserName is who it belongs to since
different users
can have different environment settings. The PATH setting is normally owned
by the "<SYSTEM>" user since it is part of the base OS.

The following will get the instance back for you and print the value of the
setting:

Set PathObj =
GetObject("WinMgmts://./root/cimv2:Win32_Environment.Name='Path',UserName='<
SYSTEM>'")

wscript.echo PathObj.VariableValue


<mat...@my-deja.com> wrote in message news:8hlj86$uhm$1...@nnrp1.deja.com...

mat...@my-deja.com

unread,
Jun 7, 2000, 3:00:00 AM6/7/00
to
It works !
The doc states that if you specifies keys that does not exists, you can
create it. How ???

In article <hWs%4.101506$55.7...@news1.sttls1.wa.home.com>,

George S. Ellis

unread,
Jun 8, 2000, 3:00:00 AM6/8/00
to
If you get bored with WMI/WBEM <g>, you can also try

================================================
Dim SysEnvironment

SysEnvironment =
WshShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session
Manager\Environment\Path")

SysEnvironment = SysEnvironment & ";.\System;%SystemDrive%\Program
Files\OurStuff\Common Files\System"

nRtn = WshShell.RegWrite("HKLM\SYSTEM\ControlSet001\Control\Session
Manager\Environment\Path",SysEnvironment,"REG_EXPAND_SZ")
================================================<mat...@my-deja.com> wrote
in message news:8hlu7i$7vv$1...@nnrp1.deja.com...

DFENS

unread,
Jun 8, 2000, 3:00:00 AM6/8/00
to
From scripting you would get the class object (Win32_Environment), not a
particular instance. Spawn a new instance of it and fill in the key
properties and what every other props you want. Then do a Put_ on that
instance.

// Get the class object itself
Set EnvVarClass = GetObject("WinMgmts://./root/cimv2:Win32_Environment")

// Make a new instance of that class
Set EnvVarInst = EnvVarClass.SpawnInstance_

// File in the key props and props of interest on that instance
EnvVarInst.Name = "TEST2"
EnvVarInst.UserName ="<SYSTEM>"
EnvVarInst.VariableValue="MyTestValue"

// Write the new instance in to WMI
EnvVarInst.Put_

Note, if you run this in a CMD shell, the variable does not show up
immediately. It will appear in the next CMD shell you run as these
variables are only read when the shell window initially starts.

0 new messages